import java.util.HashSet;

/**
 * @Author：小鹿爱吃鱼
 * @Date: 2025/5/3 16:59
 * @Description:
 */
public class 链表专用 {

    public ListNode detectCycle(ListNode head) {
        HashSet<ListNode> set = new HashSet<>();
        ListNode current = head;
        while (current != null){
            if(set.contains(current)){
                return current;
            }
            set.add(current);
            current = current.next;
        }
        return null;
    }

    public static void main(String[] args) {

    }

    private static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }


}